using System;
using System.Collections;
using UnityEngine;

public class GUIGameRoomLoader : MonoBehaviour
{
	public RectTransform GameRoomPrefab;

	public RectTransform Canvas;

	public float TimeOut = 10f;

	private bool IsRefreshing = true;

	private float timeTemp;

	private void Start()
	{
		Refresh();
	}

	private void ClearCanvas()
	{
		if (!(Canvas == null))
		{
			IEnumerator enumerator = Canvas.transform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Transform transform = (Transform)enumerator.Current;
					UnityEngine.Object.Destroy(transform.gameObject);
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}
	}

	private void Update()
	{
		if (IsRefreshing && UnitZ.gameNetwork.MatchListResponse != null && UnitZ.gameNetwork.MatchListResponse.Count > 0)
		{
			IsRefreshing = false;
		}
	}

	private void OnEnable()
	{
		Refresh();
	}

	public void Refresh()
	{
		UnitZ.gameNetwork.FindInternetMatch();
		IsRefreshing = true;
		ClearCanvas();
		StartCoroutine(LoadGameRoom());
	}

	public void DrawGameLobby()
	{
		if (Canvas == null || GameRoomPrefab == null || UnitZ.gameNetwork.MatchListResponse == null)
		{
			return;
		}
		ClearCanvas();
		for (int i = 0; i < UnitZ.gameNetwork.MatchListResponse.Count; i++)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(GameRoomPrefab.gameObject, Vector3.zero, Quaternion.identity);
			gameObject.transform.SetParent(Canvas.transform);
			GUIGameRoom component = gameObject.GetComponent<GUIGameRoom>();
			RectTransform component2 = gameObject.GetComponent<RectTransform>();
			if ((bool)component2)
			{
				RectTransform rectTransform = component2;
				Vector2 sizeDelta = GameRoomPrefab.sizeDelta;
				rectTransform.anchoredPosition = new Vector2(5f, 0f - sizeDelta.y * (float)i);
				RectTransform rectTransform2 = component2;
				float width = Canvas.rect.width;
				Vector2 sizeDelta2 = component2.sizeDelta;
				rectTransform2.sizeDelta = new Vector2(width, sizeDelta2.y);
				component2.localScale = GameRoomPrefab.gameObject.transform.localScale;
			}
			if ((bool)component.RoomName)
			{
				if (UnitZ.gameNetwork.MatchListResponse[i].isPrivate)
				{
					component.RoomName.text = UnitZ.gameNetwork.MatchListResponse[i].name + " Players " + UnitZ.gameNetwork.MatchListResponse[i].currentSize + "/" + UnitZ.gameNetwork.MatchListResponse[i].maxSize + " is Private";
				}
				else
				{
					component.RoomName.text = UnitZ.gameNetwork.MatchListResponse[i].name + " Players " + UnitZ.gameNetwork.MatchListResponse[i].currentSize + "/" + UnitZ.gameNetwork.MatchListResponse[i].maxSize;
				}
				component.Match = UnitZ.gameNetwork.MatchListResponse[i];
			}
		}
		RectTransform component3 = Canvas.gameObject.GetComponent<RectTransform>();
		RectTransform rectTransform3 = component3;
		Vector2 sizeDelta3 = component3.sizeDelta;
		float x = sizeDelta3.x;
		Vector2 sizeDelta4 = GameRoomPrefab.sizeDelta;
		rectTransform3.sizeDelta = new Vector2(x, sizeDelta4.y * (float)UnitZ.gameNetwork.MatchListResponse.Count);
	}

	private IEnumerator LoadGameRoom()
	{
		timeTemp = Time.time;
		bool timeOut = false;
		while (IsRefreshing && !timeOut)
		{
			if (Time.time > timeTemp + TimeOut)
			{
				timeOut = true;
			}
			yield return new WaitForEndOfFrame();
		}
		timeTemp = Time.time;
		DrawGameLobby();
	}
}
